***generate targeted earnings moments
use "$temp/psid_long_cleaned", clear
keep if age<=54 //limit age range
su wages if age>=36

//get skill prices
merge m:1 statefips using "$temp/skill_prices_all", keep(match) nogen

//assign people to college based on mode:
bys uniqid: egen coll_mode = mode(coll)
drop if coll_mode == .

//moments: mean and variance of normalized earnings in the early part of life cycle,
//and mean/variance of growth rate from first to second part of life cycle
//here we'll allow moving

//normalize wages
replace wages = wages if !coll //convert to real terms to account for oversampling of different locations
replace wages = wages if coll //convert to real terms to account for oversampling of different locations
preserve
	use "$temp/wage_norm", clear
	su mean
	local norm `r(mean)'
restore
replace wages = wages/`norm' //normalize by mean earnings; 43,983.68
//replace wages = wages/skill_price_2000 if !coll //normalize by mean earnings; 43,983.68
//replace wages = wages/skill_price_coll_2000 if coll //normalize by mean earnings; 43,983.68
su wages

su wages if age>=36


//generate age groups and collapse
gen age_group = 1
replace age_group = 2 if age>=36 //older age groups

qui{
forval a = 18/54{ //loop over ages
	noi di as result "Working on bins for age `a'" 
	forval y = 1970/2015{
	    		
	    preserve
		local min = `a' - 2
		local max = `a' + 2
		//local min = `y' - 2
		//local max = `y' + 2
		
		gen count = 1
		keep if year == `y' & (age>=`min' & age<=`max')
		
		count
		if `r(N)'==0{
			restore
			continue
		}
		
		//keep if age == `a' & (year>=`min' & year<=`max')
		//collapse (mean) inc_mean = log_inc (sd) inc_sd = log_inc (sum) count //[fweight = weight]
		collapse (mean) wage_mean = wages (sd) wage_sd = wages (sum) count [w=weight], by (coll) //[fweight = weight]
		gen age = `a'
		gen year = `y'
		save "$temp/bin_`a'_`y'", replace		
		restore		
	}
}


//add bins together
clear
forval a = 18/54{ //loop over ages
	forval y = 1970/2015{
	   cap append using "$temp/bin_`a'_`y'"
	   //erase "$temp/bin_`a'_`y'.dta"
	}
}
save "$temp/psid_bins", replace
}



use "$temp/psid_bins", clear
gen age_group = 1
replace age_group = 2 if age>=36

//run regressions and get target moments
reg wage_mean i.age_group i.year if !coll
local mean_earn_ncoll = _b[1.age_group] + _b[_cons]
local mean_earn_old_ncoll = _b[2.age_group] + _b[_cons]


local se_mean_ncoll = _se[_cons]
local se_mean_old_ncoll = (_se[_cons] + _se[2.age_group])/2


reg wage_mean i.age_group i.year if coll
local mean_earn_coll = _b[1.age_group] + _b[_cons]
local mean_earn_old_coll = _b[2.age_group] + _b[_cons]

local se_mean_coll = _se[_cons]
local se_mean_old_coll = (_se[_cons] + _se[2.age_group])/2

reg wage_sd i.age_group i.year if !coll
local sd_earn_ncoll = _b[1.age_group] + _b[_cons]
local sd_earn_old_ncoll = _b[2.age_group] + _b[_cons]

local se_sd_ncoll = _se[_cons]
local se_sd_old_ncoll = (_se[_cons] + _se[2.age_group])/2

reg wage_sd i.age_group i.year if coll
local sd_earn_coll = _b[1.age_group] + _b[_cons]
local sd_earn_old_coll = _b[2.age_group] + _b[_cons]

local se_sd_coll = _se[_cons]
local se_sd_old_coll = (_se[_cons] + _se[2.age_group])/2

//from previous analyses
local mean_growth_ncoll = 0.22921659
local sd_growth_ncoll = 0.35712203
local mean_growth_coll = 0.40817004
local sd_growth_coll = 0.35824144

clear
set obs 13
gen moment = .
replace moment = 0.34 in 1
replace moment = `mean_earn_ncoll' in 2
replace moment = `sd_earn_ncoll' in 3
replace moment = `mean_earn_coll' in 4
replace moment = `sd_earn_coll' in 5
replace moment = `mean_growth_ncoll' in 6
replace moment = `sd_growth_ncoll' in 7
replace moment = `mean_growth_coll' in 8
replace moment = `sd_growth_coll' in 9
replace moment = `mean_earn_old_ncoll' in 10
replace moment = `sd_earn_old_ncoll' in 11
replace moment = `mean_earn_old_coll' in 12
replace moment = `sd_earn_old_coll' in 13

save "$temp/moments", replace


clear
set obs 8
gen se = .
replace se = `se_mean_ncoll' in 1
replace se = `se_mean_old_ncoll' in 2
replace se = `se_mean_coll' in 3
replace se = `se_mean_old_coll' in 4
replace se = `se_sd_ncoll' in 5
replace se = `se_sd_old_ncoll' in 6
replace se = `se_sd_coll' in 7
replace se = `se_sd_old_coll' in 8
export delimited "$dir/Model/boot/earnings_ses_v2.csv", novarn replace
